package com.seecode;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.seecode.Adapter.BookAdapter;
import com.seecode.Database.Fuxi;
import com.seecode.Database.History;
import com.seecode.UnitClass.BookClass;
import com.seecode.Utils.Utils;

import java.util.ArrayList;
import java.util.List;

public class HistoryActivity extends AppCompatActivity {

    private GridView book;
    private BookAdapter bookAdapter;
    private ArrayList<BookClass> dataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);

        book =(GridView) findViewById(R.id.book);
        dataInit();
        bookAdapter = new BookAdapter(this,R.layout.book_item,dataList);
        book.setAdapter(bookAdapter);

        book.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                BookClass bookClass = dataList.get(position);
                String filePath = bookClass.getFilePath();
                Utils.loadFile(filePath);
                finish();


            }
        });


    }
    public void dataInit(){


        List<History> his  = Fuxi.findAll(History.class);
        Logs.i("count of data size:"+his.size());
        if(his.size()>0){

            for(History h : his){



               dataList.add(new BookClass(h.getFileName(),h.getFilePath()));

            }
        }

    }
}
